<?php
namespace app\admin\model\yunfood;

use addons\yunfood\model\Promotion as BaseModel;

class Promotion extends BaseModel
{
    // 追加属性
    protected $append = [
        'status_text',
        'type_text',
        'exclusive_text',
        'auto_receive_text',
        'time_valid' // 新增时间有效性标识
    ];
    
    // 获取时间有效性
    public function getTimeValidAttr($value, $data)
    {
        $now = time();
        return ($data['stime'] <= $now && $data['etime'] >= $now) ? 1 : 0;
    }
    
    public function getStatusList()
    {
        return ['1' => '开启', '2' => '关闭'];
    }
    
    public function getTypeList()
    {
        return ['2' => '优惠券'];
        //return ['1' => '促销', '2' => '优惠券', '3' => '团购', '4' => '秒杀'];
    }
    
    public function getExclusiveList()
    {
        return ['1' => '不排他', '2' => '排他'];
    }
    
    public function getAutoReceiveList()
    {
        return ['1' => '自动领取', '2' => '不自动领取'];
    }
    
    public function getStatusTextAttr($value, $data)
    {
        $value = $value ?: ($data['status'] ?? '');
        $list = $this->getStatusList();
        return $list[$value] ?? '';
    }
    
    public function getTypeTextAttr($value, $data)
    {
        $value = $value ?: ($data['type'] ?? '');
        $list = $this->getTypeList();
        return $list[$value] ?? '';
    }
    
    public function getExclusiveTextAttr($value, $data)
    {
        $value = $value ?: ($data['exclusive'] ?? '');
        $list = $this->getExclusiveList();
        return $list[$value] ?? '';
    }
    
    public function getAutoReceiveTextAttr($value, $data)
    {
        $value = $value ?: ($data['auto_receive'] ?? '');
        $list = $this->getAutoReceiveList();
        return $list[$value] ?? '';
    }
}